<?php  

App::import('Behavior', 'scope');

class Article extends CakeTestModel {
	var $name = 'Article';
	
	var $actsAs = array(
		'Scope'  => array(
			'enabled' => false
		),
	);
	
	var $belongsTo = array('User');
	var $hasMany = array('Comment');
}

class Comment extends CakeTestModel {
	var $name = 'Comment';

	var $actsAs = array(
		'Scope'  => array(
			'enabled' => false
		),
	);
	var $belongsTo = array('Article', 'User');
}

class User extends CakeTestModel {
	var $name = 'User';
	var $hasMany = array('Article', 'Comment');
}


class PostTest extends CakeTestModel { 
    var $name = 'Post'; 
	
	var $actsAs = array(
		'Scope'  => array(
			'enabled' => false
		),
	);
	
}

class ScopeTestCase extends CakeTestCase { 
	var $fixtures = array(
		'core.article', 'core.comment', 'core.user', //'Post'
	); 

	function endTest() {
		ClassRegistry::flush();
	}
	
	function testParameters() {
        $this->Article =& new Article(); 
		$Scope =& new ScopeBehavior();

		$config=array('enabled' => false);
		$result = $Scope->scopeSetup($this->Article,$config);
		$required=array('Article'=>array('enabled' => false, 'scope'=>array()));
		$this->assertEqual($Scope->settings,$required);

		$config=array('enabled' => true);
		$result = $Scope->scopeSetup($this->Article,$config);
		$required=array('Article'=>array('enabled' => true, 'scope'=>array()));
		$this->assertEqual($Scope->settings,$required);
		
		$config=array('user_id' => 1);
		$result = $Scope->scopeSetup($this->Article,$config);
		$required=array('Article'=>array('enabled' => true, 'scope'=>array('user_id'=>1)));
		$this->assertEqual($Scope->settings,$required);
		
	}
	
	/**
	 * Test before find callback
	 *
	 * @return void
	 * @access public
	 */
    function testFind() { 
        $this->Article =& new Article(); 
		$get=$this->Article->find('all',array('fields' => array('id'), 'recursive'=>-1));
		$required = 
			array (
				array ( 'Article' => array ('id' => 1)),
				array ( 'Article' => array ('id' => 2)),
				array ( 'Article' => array ('id' => 3)),
			)
		; 
		$this->assertEqual($required,$get);

		$this->Article->scopeSetup(array('user_id'=>1));
		$get=$this->Article->find('all',array('fields' => array('id'), 'recursive'=>-1));
		$required = 
			array (
				array ( 'Article' => array ('id' => 1)),
				array ( 'Article' => array ('id' => 3)),
			)
		; 
		$this->assertEqual($required,$get);

		$this->Article->scopeSetup(array('scope' => array('user_id'=>1)));
		$get=$this->Article->find('all',array('fields' => array('id'), 'recursive'=>-1));
		$required = 
			array (
				array ( 'Article' => array ('id' => 1)),
				array ( 'Article' => array ('id' => 3)),
			)
		; 
		$this->assertEqual($required,$get);
		
		$this->Article->scopeSetup(array('scope' => array('user_id'=>1), 'enabled'=>false));
		$get=$this->Article->find('all',array('fields' => array('id'), 'recursive'=>-1));
		$required = 
			array (
				array ( 'Article' => array ('id' => 1)),
				array ( 'Article' => array ('id' => 2)),
				array ( 'Article' => array ('id' => 3)),
			)
		; 
		$this->assertEqual($required,$get);
		
		
		$this->Article->scopeSetup(array('user_id'=>1));
		$this->Article->recursive=-1;
		$get=$this->Article->findAll(null, array('id'));
		$required = 
			array (
				array ( 'Article' => array ('id' => 1)),
				array ( 'Article' => array ('id' => 3)),
			)
		; 
		$this->assertEqual($required,$get);
	}

   
	/**
	 * Test before save callback. Create new record case
	 *
	 * @return void
	 * @access public
	 */
	function testCreate() {
       $this->Article =& new Article(); 

		$article=array ('title' => 'My Article 1', 'body' => 'My Article Body 1', 'published' => 'N', 'created' => '2007-11-18 10:41:23', 'updated' => '2007-11-18 10:43:31');

		$this->Article->scopeSetup(array('user_id'=>2));
		$this->Article->create(		
			array ('title' => 'My Article 1', 'body' => 'My Article Body 1', 'published' => 'N', 'created' => '2007-11-18 10:41:23', 'updated' => '2007-11-18 10:43:31')
		);
		$this->Article->save();
		$required=array('Article'=>$article);
		$required['Article']['user_id']=2;
		$required['Article']['id']=$this->Article->id;
		$this->Article->recursive=-1;
		$get=$this->Article->read(null,$this->Article->id);
		$this->assertEqual($required,$get);
		
		
		$this->Article->create(		
			array ('user_id'=>999,'title' => 'My Article 1', 'body' => 'My Article Body 1', 'published' => 'N', 'created' => '2007-11-18 10:41:23', 'updated' => '2007-11-18 10:43:31')
		);
		$this->Article->save();
		$required=array('Article'=>$article);
		$required['Article']['user_id']=2;
		$required['Article']['id']=$this->Article->id;
		$this->Article->recursive=-1;
		$get=$this->Article->read(null,$this->Article->id);
		$this->assertEqual($required,$get);

		
	} 
	
	/**
	 * Test before save callback. Update record case
	 *
	 * @return void
	 * @access public
	 */
	function testUpdate() {
       $this->Article =& new Article(); 


		$this->Article->scopeSetup(array('user_id'=>1));
		$this->Article->read(null, 1);
		$this->Article->set('title','Features');
		$this->Article->save();
		$this->Article->recursive=-1;
		$get=$this->Article->read(null, 1);
		$required=array('Article' => array ('id' => 1, 'user_id' => 1, 'title' => 'Features', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'));
		$this->assertEqual($required,$get);
	   
		$this->Article->scopeSetup(array('user_id'=>1));
		$this->Article->read(null, 1);
		$this->Article->set('user_id',2);
		$this->Article->set('title','Features');
		$this->Article->save();
		$this->Article->recursive=-1;
		$get=$this->Article->read(null, 1);
		$required=array('Article' => array ('id' => 1, 'user_id' => 1, 'title' => 'Features', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'));
		$this->assertEqual($required,$get);
	} 
	
	/**
	 * Test before delete callback. 
	 *
	 * @return void
	 * @access public
	 */
	function testDelete() {
		$this->Article =& new Article(); 
		$this->Article->scopeSetup(array('user_id'=>1));
		$this->Article->del(1);
		$get=$this->Article->read(null, 1);
		$this->assertFalse($get);

		$this->Article->del(2);
		$get=$this->Article->read(null, 2);
		$this->assertFalse($get);

		
		$this->Article->scopeDisable();
		$this->Article->recursive=-1;
		$get=$this->Article->read(null, 1);
		$this->assertFalse($get);
		$get=$this->Article->read(null, 2);
		$required=array('Article' => array ('id' => 2, 'user_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'));
		$this->assertEqual($get,$required);		
	} 
	

	

} 
?>